package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceServe;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServeImpl implements WorkspaceServe {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public BusinessDataVO getBusinessData() {
        //获取今日截至到现在的时间
        LocalDate now = LocalDate.now();
        LocalDateTime beginTime=LocalDateTime.of(now, LocalTime.MIN);//获取今天的开始时刻
        LocalDateTime endTime=LocalDateTime.of(now, LocalTime.MAX);//获取今天的结束时刻
        Integer newUsers = userMapper.getNewUser(beginTime, endTime);//今日新增用户
        Map<String,Object> map = new HashMap<>();
        map.put("le",beginTime);
        map.put("ri",endTime);
        Integer total = orderMapper.ordersStatistics(map);//获取今日全部订单
        map.put("status",5);
        Integer valid = orderMapper.ordersStatistics(map);//获取今日有效订单
        double completionRate=0.0;//订单完成率
        if(total!=null&&total>0){
            completionRate=valid*1.0/total;
        }
        Double turnover = orderMapper.getRurnoverByDate(beginTime, endTime);//获取今日营业额
        double unitPrice=turnover/valid;//获取今日平均客单价
        return new BusinessDataVO(turnover,valid,completionRate,unitPrice,newUsers);
    }

    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Integer discontinued= setmealMapper.getIsOrNotSoldCount(0);//停售的数量
        Integer sold= setmealMapper.getIsOrNotSoldCount(1);//起售的数量
        return new SetmealOverViewVO(sold,discontinued);
    }

    @Override
    public DishOverViewVO getOverviewDishes() {
        Integer discontinued= dishMapper.getIsOrNotSoldCount(0);//停售的数量
        Integer sold= dishMapper.getIsOrNotSoldCount(1);//起售的数量
        return new DishOverViewVO(sold,discontinued);
    }

    @Override
    public OrderOverViewVO getOverviewOrders() {
        Map<String,Object> map = new HashMap<>();
        Integer totals= orderMapper.ordersStatistics(map);
        map.put("status",Orders.CANCELLED);
        Integer cancels=orderMapper.ordersStatistics(map);
        map.put("status",Orders.COMPLETED);
        Integer completions=orderMapper.ordersStatistics(map);
        map.put("status",Orders.CONFIRMED);
        Integer delivers=orderMapper.ordersStatistics(map);
        map.put("status",Orders.TO_BE_CONFIRMED);
        Integer waits=orderMapper.ordersStatistics(map);
        return new OrderOverViewVO(waits,delivers,completions,cancels,totals);
    }
}
